import 'package:flutter/material.dart';
import 'package:flutter_shop/entity/select_model.dart';
import 'package:flutter_shop/res/colors.dart';
import 'package:flutter_shop/res/dimens.dart';
import 'package:flutter_shop/res/styles.dart';
import 'package:flutter_shop/widget/water_click.dart';

///常规列表选择--------------------------------------------------------------------------------
class ListSelectSheet extends StatelessWidget {
  final List<SelectModel> list;
  final bool isMulti;
  final String title;
  final Function callback;

  ListSelectSheet(
    this.list, {
    this.isMulti = false,
    this.title = "请选择",
    this.callback,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Container(
            width: double.infinity,
            padding: Paddings.common,
            decoration: BoxDecoration(
              color: CommonColors.primary,
              borderRadius: BorderRadius.only(topLeft: Radius.circular(10), topRight: Radius.circular(10)),
            ),
            child: Text(
              title,
              style: TextStyle(fontSize: CommonDimens.font_size_large, color: Colors.white),
            ),
          ),
          ListView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemCount: list.length,
            itemBuilder: _buildItem,
          ),
          Offstage(
            offstage: !isMulti,
            child: WaterClick(
              width: double.infinity,
              padding: Paddings.common,
              fillColor: Colors.white,
              onPressed: () {
                if (isMulti) {
                  List<SelectModel> val = list.where((v) => v.checked).toList();
                  callback(val);
                  Navigator.pop(context);
                }
              },
              child: Text("确定", style: TextStyle(color: CommonColors.primary)),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildItem(BuildContext context, int index) {
    SelectModel item = list[index];

    Widget unchecked = Icon(Icons.radio_button_unchecked, color: CommonColors.gray, size: 15);
    Widget checked = Icon(Icons.check_circle, color: CommonColors.primary, size: 15);

    return StatefulBuilder(builder: (context, setState) {
      return WaterClick(
        padding: Paddings.common,
        alignment: Alignment.centerLeft,
        decoration: BoxDecoration(
          color: Colors.white,
          border: Borders.bottom,
        ),
        child: Row(
          children: <Widget>[
            Expanded(
              child: Text(
                item.label,
                style: TextStyles.common_text,
              ),
            ),
            item.checked ? checked : unchecked,
          ],
        ),
        onPressed: () {
          if (!isMulti) {
            callback(item);
            Navigator.pop(context);
            return;
          }

          setState(() {
            item.checked = !item.checked;
          });
        },
      );
    });
  }
}
